package com.car.loan.project.xr.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author huangding
 * @date 2020/2/19 14:39
 */
@Component
@Slf4j
public class ExcelFileUtil {

    /**
     * 静态资源路径
     */
    @Value("${file_path.excel}")
    private String staticFilePath;


    public InputStream getTemplateInputStream(String filename) {
        String path = staticFilePath + File.separator + "statistics"
            + File.separator + filename;
        File file = new File(path);
        if (!file.isFile()) {
            throw new NullPointerException("不存在该模板");
        }
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new NullPointerException("模板读取错误" + e.getMessage());
        }
    }

    public String getTemplatePath(String filename) {
        return staticFilePath + File.separator + "statistics"
            + File.separator + filename;
    }
}
